﻿<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>用户管理</title>
    <jsp:include page="/resources/inc/head.jsp"/>
</head>
<body>
<div id="main">
    <div id="toolbar">
        <shiro:hasPermission name="upms:user:create">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="Common.openPage({url:'${basePath}/manage/user/create',title:'新增用户'})">
                <i class="zmdi zmdi-plus"></i>新增用户</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:user:update">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="Common.toolBarUpdate({url:'${basePath}/manage/user/update/',id:'userId',title:'编辑用户'})">
                <i class="zmdi zmdi-edit"></i>编辑用户</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:user:delete">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="Common.batchDelete({url:'${basePath}/manage/user/delete/',id:'userId'})">
                <i class="zmdi zmdi-close"></i>删除用户</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:user:role">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="roleAction()">
                <i class="zmdi zmdi-accounts"></i>用户角色</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:user:permission">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="permissionAction()">
                <i class="zmdi zmdi-key"></i>用户权限</a>
        </shiro:hasPermission>
        <shiro:hasPermission name="upms:user:update">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="resetPassword()">
                <i class="zmdi zmdi-refresh"></i>重置密码</a>
        </shiro:hasPermission>
    </div>
    <table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp"/>
<script>
    var $table = $('#table');
    $(function () {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/user/list',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            detailFormatter: 'detailFormatter',
            pagination: true,
            paginationLoop: false,
            queryParamsType: "",
            sidePagination: 'server',
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'userId',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'userId', title: '编号', sortable: true, align: 'center'},
                {field: 'username', title: '帐号'},
                {field: 'realname', title: '姓名'},
                {field: 'avatar', title: '头像', align: 'center', formatter: 'avatarFormatter'},
                {field: 'phone', title: '电话'},
                {field: 'email', title: '邮箱'},
                {field: 'locked', title: '状态', sortable: true, align: 'center', formatter: 'lockedFormatter'},
                {
                    field: 'action',
                    title: '操作',
                    align: 'center',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    clickToSelect: false
                }
            ]
        });
    });

    // 格式化操作按钮
    function actionFormatter(value, row, index) {

    }
    // 格式化图标
    function avatarFormatter(value, row, index) {
        if(value){
            return '<img src="${basePath}' + value + '" style="width:20px;height:20px;"/>';
        }
    }
    // 格式化状态
    function lockedFormatter(value, row, index) {
        if (value == 1) {
            return '<span class="label label-default">锁定</span>';
        } else {
            return '<span class="label label-success">正常</span>';
        }
    }

    // 用户角色
    function roleAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            layer.msg('请选择一条记录');
        } else {
            var roleUserId = rows[0].userId;
            Common.openPage({url:'${basePath}/manage/user/role/'+roleUserId,title:'用户角色'});
        }
    }
    // 用户权限
    function permissionAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            layer.msg('请选择一条记录');
        } else {
            var permissionUserId = rows[0].userId;
            Common.openPage({url:'${basePath}/manage/user/permission/'+permissionUserId,title:'用户授权',isMaxMin:true,isFull:true});
        }
    }

    function resetPassword(){
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            layer.msg('请选择一条记录');
        } else{
            layer.confirm('确定重置该用户密码么？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'GET',
                    url: "${basePath}/manage/user/resetPassword/" + rows[0].userId,
                    success: function (res) {
                        if (res.code != 1) {
                            if (typeof(res.data) != "undefined" && res.data.length > 0) {
                                layer.msg(res.data, {icon: 5});
                            } else {
                                layer.msg(res.message, {icon: 5});
                            }
                        } else {
                            layer.msg('密码重置成功！默认密码为：'+res.data, {icon: 1});
                            $table.bootstrapTable('refresh');
                            if (typeof (afterDelete) == "function") {
                                afterDelete();
                            }
                        }
                    },
                    error: function (res) {
                        layer.msg('密码重置失败！', {icon: 5});
                    }
                });
            }, function () {
            });
        }
    }
</script>
</body>
</html>